#!/bin/bash
if [ ! -f "ILSVRC2012_img_train.tar" ];then
echo "ILSVRC2012_img_train.tar no exist"
exit
fi

if [ ! -f "ILSVRC2012_img_val.tar" ];then
echo "ILSVRC2012_img_val.tar no exist"
exit
fi

mkdir -p train val bbox imagenet_tf
tar -xvf ILSVRC2012_img_train.tar -C train/
tar -xvf ILSVRC2012_img_val.tar -C val/
tar -xvf ILSVRC2012_bbox_train_v2.tar.gz -C bbox/

cd train/
find . -name "*.tar" | while read LINE ; do mkdir -p "${LINE%.tar}"; tar -xvf "${LINE}" -C "${LINE%.tar}"; rm -f "${LINE}"; done
cd ..
if [ -d "ImageNetPreprocess" ];then
python3 ImageNetPreprocess/preprocess_imagenet_validation_data.py val/ ImageNetPreprocess/imagenet_2012_validation_synset_labels.txt
python3 ImageNetPreprocess/build_imagenet_data.py --output_directory=imagenet_tf --train_directory=train --validation_directory=val
elif [ -d "ImageNetPreprocess-master" ];then
python3 ImageNetPreprocess-master/preprocess_imagenet_validation_data.py val/ ImageNetPreprocess-master/imagenet_2012_validation_synset_labels.txt
python3 ImageNetPreprocess-master/build_imagenet_data_master.py --output_directory=imagenet_tf --train_directory=train --validation_directory=val
else
echo "pass"
fi